package models;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Where;

import play.db.jpa.Model;

/**
 * 分类管理
 * @author zhangpeng
 *
 */

@Entity
@Table(name="category")
public class Category extends Model{
	
	
	/**
	 * 分类标题
	 */
	public String categoryTitle;
	
	/**
	 * 分类的父Id
	 * default:默认分类
	 * channel:频道
	 */
	@OneToOne
	public Category parentCategory;
	
	/**
	 * 模块名称
	 */
	public String moduleName;
	
	/**
	 * 是否是频道
	 */
	public Boolean isChannel = false; 
	
	/**
	 * 分类排序
	 */
	public Integer categoryOrder;

	/**
	 * 创建时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	public Date createDate = new Date();
	
	@OneToMany(mappedBy="articleCategory",fetch=FetchType.LAZY)
	@Where(clause="articleAuditstatus=1 and articleHidden=0")
	@OrderBy(value="createDate DESC")
	@BatchSize(size=5)
	public List<Article> articles;
	
	
	/**
	 * 获取全部频道
	 * @return
	 */
	public static List<Category> getChannelList(){
		return Category.find("byIsChannel", true).fetch();
	}
	
	/**
	 * 获取全部子分类
	 * @param parentId
	 * @return
	 */
	public static List<Category> getChildCategory(long parentId){
		return Category.find("byParentCategory.id",parentId).fetch();
	}
	
	
	public String toString(){
		return categoryTitle;
	}
	

}
